package com.zby.scms.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUtil {

    private Logger logger = LoggerFactory.getLogger(FileUtil.class);
    @Value ("${web.upload-path}")
    private String filePath;
    @Value("${web.domain}")
    private String domain;

    public boolean mkdirsDirectory(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return file.mkdirs();
        }
        return true;
    }

    //单文件上传
    public String uploadFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();  // 文件名
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        // 上传后的路径
        logger.info("上传的文件名为："+fileName);
        String formatPath = filePath;

        File dest = new File(formatPath + fileName);//文件上传的目的地
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return String.format("%s/%s", domain, fileName);
    }

    //多文件上传

}